﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Caliburn.Micro;
using ReactiveUI;
using ReactiveUI.Xaml;

namespace PurchaseSales.Manage.ViewModels
{

    public interface IShowDialogViewModel<T>
    {
        UserControl ShowDialogContent { get; set; }
        T Entity { get; set; }
        object ViewModel { get; set; }
        void CanConfirm(bool b);
    }
    public class ShowDialogViewModel<T> : ReactiveValidatedObject, IShowDialogViewModel<T> where T : class
    {
        private UserControl _showDialogContent;
        private T _entity;

        public UserControl ShowDialogContent
        {
            get { return _showDialogContent; }
            set { _showDialogContent = value; }
        }

        public T Entity
        {
            get { return _entity; }
            set { _entity = value; }
        }

        public object ViewModel { get; set; }

        public ReactiveAsyncCommand ConfromCommand { get; set; }
        public ShowDialogViewModel()
        {
            ConfromCommand = new ReactiveAsyncCommand(this.WhenAny(x => x.CanSave, x => x.Value));
            ConfromCommand.Subscribe(o =>
                                                   {
                                                       var window = o as Window;
                                                       if (window != null)
                                                       {
                                                           window.DialogResult = true;
                                                           window.Close();
                                                       }
                                                   });
        }

        public void CanConfirm(bool b)
        {
            CanSave = b;
        }

        private bool _canSave;
        public bool CanSave
        {
            get { return _canSave; }
            set { this.RaiseAndSetIfChanged(x => x.CanSave, ref _canSave, value); }
        }
    }
}
